﻿using FinancialReportPowerPointAddIn.Helper;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FinancialReportPowerPointAddIn.PPTModule
{
    public static class GenerateGuaranteedRevenue
    {
        public static void Write(int year, int month)
        {
            //获取当前ppt中所有的幻灯片
            var slides = Globals.ThisAddIn.Application.ActivePresentation.Slides;
            //在当前选中幻灯片前插入新的幻灯片--代码提示中没有F12进去才找到的方法
            Slide currentslide = slides.Add(slides.Count + 1, PpSlideLayout.ppLayoutBlank);
            currentslide.Select();
            //写入标题           
            Shape titleBox = currentslide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 50, 35, 370, 15);
            titleBox.TextFrame.TextRange.Text = "餐厅保本月营业额变化";
            titleBox.TextFrame.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoCTrue;
            titleBox.TextFrame.TextRange.Font.Size = 24f;
            //写入表格            
            Shape tableShape = currentslide.Shapes.AddTable(9, 5, 50, 80, currentslide.Master.Width, currentslide.Master.Height - 100);
            Table table = tableShape.Table;
            // 编写标题行
            int currentCol = 1;
            table.Cell(1, currentCol++).Shape.TextFrame.TextRange.Text = "";
            table.Cell(1, currentCol++).Shape.TextFrame.TextRange.Text = "保本营业额";
            table.Cell(1, currentCol++).Shape.TextFrame.TextRange.Text = "月均营业额";
            table.Cell(1, currentCol++).Shape.TextFrame.TextRange.Text = $"{month}月份营业额";
            table.Cell(1, currentCol++).Shape.TextFrame.TextRange.Text = "差距";
            //开始写入数据
            string[] restaurants = new string[] { "汇总", "潭江店", "开大店", "白石店", "西粤店", "东湖店", "恩平店", "油十店" };
            //写入MTD  要调转颜色RGB =>BGR            
            int currentrow = 2;
            using (var db = new FinancialReportContext())
            {
                foreach (string item in restaurants)
                {
                    currentCol = 1;
                    table.Cell(currentrow, currentCol++).Shape.TextFrame.TextRange.Text = item == "汇总" ? "诚福公司" : item;                   
                    string flag = string.Empty;
                    string code = string.Empty;
                    Common.GetPLNameAndCode(item, out flag, out code);
                    PLData mtdpladata = db.PLDatas.FirstOrDefault(x => x.IsPlan == false && x.Restaurant == flag && x.Year == year && x.Month == month);
                    List<PLData> ytdplapdata = db.PLDatas.Where(x => x.IsPlan == false && x.Restaurant == flag && x.Year == year && x.Month <= month).ToList();
                    //写入保本营业额
                    decimal 保本营业额 = GuaranteedRevenue.GetGuaranteedRevenueList().FirstOrDefault(x => x.Key == flag).Value;
                    table.Cell(currentrow, currentCol++).Shape.TextFrame.TextRange.Text = 保本营业额.ToString("#,##0");                    
                    //写入月均营业额                   
                    table.Cell(currentrow, currentCol++).Shape.TextFrame.TextRange.Text = (ytdplapdata.Sum(x => x.营业净收入)/month).ToString("#,##0");                    
                    //写入当月营业额和差异
                    decimal 当月营业额 = mtdpladata.营业净收入;
                    if (当月营业额 > 保本营业额)
                    {
                        table.Cell(currentrow, currentCol).Shape.TextFrame.TextRange.Text = "▲" + 当月营业额.ToString("#,##0");                        
                        table.Cell(currentrow, currentCol++).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 205, 0).ToArgb();
                        table.Cell(currentrow, currentCol).Shape.TextFrame.TextRange.Text = (当月营业额 - 保本营业额).ToString("#,##0");                        
                    }
                    else 
                    {
                        table.Cell(currentrow, currentCol).Shape.TextFrame.TextRange.Text = "▼" + 当月营业额.ToString("#,##0");                        
                        table.Cell(currentrow, currentCol++).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 0, 255).ToArgb();
                        table.Cell(currentrow, currentCol).Shape.TextFrame.TextRange.Text = (当月营业额 - 保本营业额).ToString("#,##0");
                        table.Cell(currentrow, currentCol).Shape.TextFrame.TextRange.Font.Color.RGB = Color.FromArgb(0, 0, 255).ToArgb();
                    }                    
                    //
                    currentrow++;
                }
            }
            Common.AutoFit(table);
            Common.OrganizeFormat(table,24,18);
            
            //Common.OrganizeFormat2(table);
            // 设置整个表格的单元格边距为无            
            tableShape.Height = currentslide.Master.Height - 100;
            tableShape.Width = currentslide.Master.Width - 90;
        }        
    }
}
